/**
 * @Author: Nico
 */
//必须带done回调 执行完成调用
import { Toast } from 'vant'
import isEmpty from 'lodash/isEmpty'
import { playError } from '../audio/webAudio'

import { translateTitle } from '@/utils/i18n'

export default function validateForm({ vm, action, done, eventName }) {
  console.log('wyq', vm, action, eventName)
  if (action.extension.formSelect.value.length > 0) {
    // 修改为验证多个表单
    let flag = true
    // action.extension.formSelect.value.forEach((item) => {

    // })
    for (let item of action.extension.formSelect.value) {
      const _self = vm.vuiComponents[item] && vm.vuiComponents[item].self
      if (!_self) return
      const eachFunc = (obj) => {
        // 必输校验
        if (!flag) return
        if (obj.type != 'uiPdaButton' && !isEmpty(obj.extension.extendRule) && !isEmpty(obj.extension.extendRule.value)) {
          if (obj.type == 'uiPdaUpload') {
            //
            const self = vm.vuiComponents[obj._id]
            if (obj.extension.extendRule.value.ChangeSwitch && obj.extension.extendRule.value.required && self.self.fileList.length == 0) {
              Toast(`请上传文件`)
              return
            }
          } else {
            if (obj.extension.extendRule.value.required && !_self.selfFormData[obj.extension.model.value]) {
              const formItem = vm.vuiComponents[obj._id] //获取选中得组件
              formItem.ref?.focus() //组件聚焦
              Toast(`${translateTitle('Ve.elements.请输入')}${obj.extension.label.value}`)
              playError()
              flag = false
              vm[`${eventName}IsRunning`] = false
              return
            }
          }
        }
        if (obj.list) {
          for (let item of obj.list) {
            eachFunc(item)
          }
        }
        if (obj.pageModules) {
          for (let item of obj.pageModules) {
            eachFunc(item)
          }
        }
      }

      eachFunc(_self.info)
      if (flag) {
        done(null, {})
      }
    }
  } else {
    done(null, {})
  }
}
